package com.inossem.warehousestorage_inossem.activity.out_storage;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.inossem.loadandrefresh.PullToRefreshLayout;
import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.QRCodeUtils;
import com.inossem.utils.base.Utils;
import com.inossem.utils.constant.QRCodeConstant;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.utils.log.LogUtils;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.activity.out_storage.extra.OutStorageExtra;
import com.inossem.warehousestorage_inossem.adapter.outStorage.PickingOutStorageItemListAdapter;
import com.inossem.warehousestorage_inossem.bean.outStorage.ItemListBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStorageConfirmBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingConfirmRequestBean;
import com.inossem.warehousestorage_inossem.bean.outStorage.OutStoragePickingOverviewBean;
import com.inossem.warehousestorage_inossem.dialog.DialogUtils;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStoragePickingConfirmRequest;
import com.inossem.warehousestorage_inossem.request.outStorage.OutStoragePickingOverviewRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.OutStorageUtils;
import com.inossem.warehousestorage_inossem.utils.SPUtils;

import java.util.List;


//验收入库
public class OutStoragePickingOverviewActivity extends BaseActivity {

    private TextView title, nextStep;

    private PullToRefreshLayout refresh;

    private PullableListView pickingOutStorageItemList;

    private List<ItemListBean> allData;

    private PickingOutStorageItemListAdapter adapter;

    private String mOutStoragePickingCode, mMaterielCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_out_storage_picking_overview);
        initView(OutStoragePickingOverviewActivity.this);
        SPUtils.removePickingOutStorageInformation(OutStoragePickingOverviewActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(OutStoragePickingOverviewActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        pickingOutStorageItemList = (PullableListView) activity.findViewById(R.id.pickingOutStorageItemList);
        refresh = (PullToRefreshLayout) activity.findViewById(R.id.pickingOutStorageItemListRefresh);

        nextStep = (TextView) activity.findViewById(R.id.nextStep);
    }

    private void initData(Activity activity) {
//        title.setText(getResources().getString(R.string.activity_out_storage_picking_add_operator_an_overview_of_the_acquisition));
        ActivityUtils.initBackAndFunction(activity, getResources().getString(R.string.activity_out_storage_picking_overview_picking), new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        pickingOutStorageItemList.setPullUpFlag(false);
        pickingOutStorageItemList.setPullDownFlag(false);
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                //过账拼接JSON内容
                if (check(activity)) {
                    confirm(activity);
                }
            }
        });
        mMaterielCode = "";
        getExtra(activity);
        getData(activity);
        setmScanListener((String scanResult, int length) -> {
            mMaterielCode = QRCodeUtils.DecryptQRCode(scanResult);
            getData(activity);
        }, QRCodeConstant.TYPE_MATERIEL_CODE);
    }

    private void confirm(Context context) {
        OutStoragePickingConfirmRequestBean params = OutStorageUtils.getOutStoragePickingConfirmRequestBean(context);
        OutStoragePickingConfirmRequest.request(context, params, new GsonBeanListener<OutStorageConfirmBean>() {
            @Override
            public void onSuccess(OutStorageConfirmBean bean) {
                LogUtils.i("onSuccess:" + bean.getBody());
                SPUtils.removePickingOutStorageInformation(context);
                if (!TextUtils.isEmpty(bean.getHead().getMsg())) {
                    Utils.middleToast(context, bean.getHead().getMsg());
                }
                Intent intent = new Intent(context, OutStorageResultActivity.class);
                startActivity(intent);
            }

            @Override
            public void onFailing(OutStorageConfirmBean bean) {
                LogUtils.i("onFailing:" + bean.getBody());
                if (!TextUtils.isEmpty(bean.getBody())) {
                    OutStorageUtils.updatePickingOutStorageNumber(context, bean.getBody());
                }
            }
        });
    }

    private boolean check(Context context) {
        int confirmNumber = OutStorageUtils.getPickingOutStorageNumber(context);
        if (confirmNumber == 0) {
            Utils.middleToast(context, getResources().getString(R.string.activity_in_storage_free_overview_at_least_one_item));
            return false;
        }
        return true;
    }

    private void getExtra(Activity activity) {
        mOutStoragePickingCode = activity.getIntent().getStringExtra(OutStorageExtra.OUT_STORAGE_PICKING_NUMBER);
    }

    private void addOutStoragePickingItemListData(Context context, OutStoragePickingOverviewBean.BodyBean bean, List<ItemListBean> receiveInStorageItemList, ListView listView, boolean flag) {
        if (flag) {
            adapter = new PickingOutStorageItemListAdapter(context, bean, receiveInStorageItemList, mMaterielCode, (String materielCode) -> {
                mMaterielCode = materielCode;
                getData(context);
            });
            listView.setAdapter(adapter);
        }
    }

    private void getData(Context context) {
        String pickingOutStorageInformation = SPUtils.getPickingOutStorageInformation(context);
        if (TextUtils.isEmpty(pickingOutStorageInformation)) {
            OutStoragePickingOverviewRequest.request(context, mOutStoragePickingCode, new GsonBeanListener<OutStoragePickingOverviewBean>() {
                @Override
                public void onSuccess(OutStoragePickingOverviewBean bean) {
                    Gson gson = new Gson();
                    String json = gson.toJson(bean, OutStoragePickingOverviewBean.class);
                    SPUtils.savePickingOutStorageInformation(context, json);
                    OutStoragePickingOverviewBean.BodyBean data = bean.getBody();
                    allData = bean.getBody().getItem_list();
                    title.setText(bean.getBody().getRefer_receipt_code());
                    addOutStoragePickingItemListData(context, data, ActivityUtils.getShowData(allData, 0), pickingOutStorageItemList, true);
                }
            });
        } else {
            Gson gson = new Gson();
            OutStoragePickingOverviewBean bean = gson.fromJson(pickingOutStorageInformation, OutStoragePickingOverviewBean.class);
            if (TextUtils.isEmpty(mMaterielCode)) {
                allData = bean.getBody().getItem_list();
            } else {
                allData = OutStorageUtils.getPickingOutStorageList(context, mMaterielCode);
            }
            title.setText(bean.getBody().getRefer_receipt_code());
            addOutStoragePickingItemListData(context, bean.getBody(), ActivityUtils.getShowData(allData, 0), pickingOutStorageItemList, true);
        }
    }

    public interface OutStoragePickingOverviewListener {
        void onNoDoubleClickListener(String materielCode);
    }

    @Override
    public void onBackPressed() {
        DialogUtils.getConfirmDialog(OutStoragePickingOverviewActivity.this, getResources().getString(R.string.activity_storage_manage_clear_prompt), new DialogUtils.ConfirmDialogListener() {
            @Override
            public void onConfirm(Dialog dialog) {
                OutStoragePickingOverviewActivity.super.onBackPressed();
            }
        }).show();
    }
}
